package com.xxd.spring.boot.demo.lihonghai.uti;
import com.xxd.spring.boot.demo.lihonghai.entity.Student;

import java.sql.*;


public class JDBC {
    public static void chaxun(){
        ResultSet resultSet=null;
        PreparedStatement statement=null;
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user="root";
            String pwd="dh15215186339";
            String url="jdbc:mysql://localhost:3306/stumanager?serverTimezone=UTC";
            connection= DriverManager.getConnection(url,user,pwd);

            String sql="select * from user";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void insert(Student student){
        ResultSet resultSet=null;
        Statement statement=null;
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user="root";
            String pwd="dh15215186339";
            String url="jdbc:mysql://localhost:3306/stumanager?serverTimezone=UTC";
            connection= DriverManager.getConnection(url,user,pwd);
            String sql="INSERT INTO stumanager.user(num,name,sex,address)VALUES('"+ student.getNum()+"','"+ student.getName()+"','"+ student.getSex()+"','"+ student.getAddress()+"')";
            statement = connection.createStatement();
            int n=statement.executeUpdate(sql);
           if(n>0)
           {System.out.println("添加成功");
           }
           else{
               System.out.println("添加失败");
           }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
